package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card{
    private String suit;//花色
    private int rank;//数值

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "suit='" + suit + '\'' +
                ", rank='" + rank + '\'' +
                '}';
    }
}

public class TestCard {
    public static final String[] suits = {"♥","♠","♣","♦"};

    public static List<Card> buyCard(){
        List<Card> dest = new ArrayList<>();
        for(int i = 0 ; i < 4; i++){
            for(int j = 1; j <= 13; j ++){
                String suit = suits[i];
                Card card = new Card(suit, j);
                dest.add(card);
            }
        }
        return dest;
    }

    public static void shuffle(List<Card> cardList){
        for(int i = cardList.size()-1; i > 0; i--){
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardList, i, index);
        }
    }
    //set的方法就是把pos位置的元素设为value;
    private static void swap(List<Card> cardList, int i, int j){
        Card tmp = cardList.get(i);
        cardList.set(i, cardList.get(j));
        cardList.set(j, tmp);
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        System.out.println("买牌" + cardList);
        shuffle(cardList);
        System.out.println("洗牌：" + cardList);

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hands = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 3; j++){
                Card card = cardList.remove(0);
                List<Card> hand = hands.get(j);
                //hand.set(i, card); 不能是set 因为set是往数组更新
                hand.add(i, card);
            }
        }
        System.out.println("第一个人的牌" + hand1);
        System.out.println("第二个人的牌" + hand2);
        System.out.println("第三个人的牌" + hand3);
        System.out.println("剩余的牌" + cardList);
    }
}